
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .layui-table select {
                position: relative; /* Ensure proper positioning */
                z-index: 9999; /* Set a high z-index to display above other elements */
            }

            .layui-table td {
                overflow: visible; /* Allow dropdowns to overflow table cells */
            }
        </style>
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" lay-filter="search-form" id="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="user_id" placeholder="请输入用户ID" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">作品名称</label>
                            <div class="layui-input-inline">
                                <input type="hidden" name="name[]" value="like">
                                <input type="text" name="name[]" placeholder="请输入作品名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">作品状态</label>
                            <div class="layui-input-inline">
                                <select name="status">
                                    <option value="">全部</option>
                                    <option value="0">未审核</option>
                                    <option value="1">审核通过</option>
                                    <option value="2">审核未通过</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">公开状态</label>
                            <div class="layui-input-inline">
                                <select name="is_public">
                                    <option value="">全部</option>
                                    <option value="1">公开</option>
                                    <option value="0">私有</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否精选</label>
                            <div class="layui-input-inline">
                                <input type="hidden" name="is_recommend" value="0">
                                <input type="checkbox" lay-skin="switch" lay-text="是|否" lay-filter="isRecommendSwitch1">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否购买素材</label>
                            <div class="layui-input-inline">
                                <input type="hidden" name="is_buy" value="0">
                                <input type="checkbox" lay-skin="switch" lay-text="是|否" lay-filter="isBuySwitch1">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" lay-submit lay-filter="table-query">搜索</button>
                            <button type="reset" class="layui-btn layui-btn-primary" lay-submit lay-filter="table-reset">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.work.update">编辑</button>
        </script>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/work/select";
            const UPDATE_API = "/app/admin/work/update";
            const SET_STATUS_API = "/app/admin/work/setStatus";
            const DELETE_API = "/app/admin/work/delete";
            const INSERT_URL = "/app/admin/work/insert";
            const UPDATE_URL = "/app/admin/work/update";
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;

                // Listen for changes to the "是否精选" switch
                form.on("switch(isRecommendSwitch1)", function(data) {
                    let value = data.elem.checked ? 1 : 0; // Set value to 1 if checked, otherwise 0
                    document.querySelector("input[name='is_recommend']").value = value;
                });

                form.on("switch(isBuySwitch1)", function(data) {
                    let value = data.elem.checked ? 1 : 0; // Set value to 1 if checked, otherwise 0
                    document.querySelector("input[name='is_buy']").value = value;
                });

				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					},{
						title: "id",align: "center",
						field: "id",
					},{
						title: "用户ID",align: "center",
						field: "user_id",
					},{
						title: "作品名称",align: "center",
						field: "name",
					},{
						title: "作品主图",align: "center",
						field: "thumb",
                        templet: function(d) {
                            return '<img src="' + d.thumb + '" style="width: 50px; height: 50px;" />';
                        }
					},{
						title: "作品图集",align: "center",
						field: "images",
                        templet: function(d) {
                            let images = JSON.parse(d.images)
                            let html = "";
                            for (let i = 0; i < images.length; i++) {
                                html += '<img src="' + images[i] + '" style="width: 50px; height: 50px;" />';
                            }
                            return html;
                        }
					},{
                        title: "状态", align: "center", field: "status_text",
                    },{
						title: "审核时间",align: "center",
						field: "audit_time",
					},{
						title: "审核理由",align: "center",
						field: "reason",
					},{
						title: "查看次数",align: "center",
						field: "views",
					},{
						title: "购买次数",align: "center",
						field: "sales",
					},{
						title: "是否公开",align: "center",
						field: "is_public",
					},{
						title: "创建时间",align: "center",
						field: "created_time",
                        width: 200
					},{
						title: "价格(点击可直接修改)",align: "center",
						field: "price",
                        edit: 'text'
					},{
                        title: "是否精选", align: "center", field: "is_recommend",
                        templet: function(d) {
                            return '<input type="checkbox" lay-skin="switch" lay-text="是|否" lay-filter="isRecommendSwitch" ' + (d.is_recommend ? 'checked' : '') + ' data-id="' + d.id + '">';
                        }
                    },{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 120,
					}
				];

                form.on('switch(isRecommendSwitch)', function(obj) {
                    let id = obj.elem.getAttribute('data-id'); // 获取当前行的 ID
                    let isRecommend = obj.elem.checked ? 1 : 0; // 获取开关状态

                    // 调用接口保存更改
                    $.ajax({
                        url: SET_STATUS_API,
                        type: 'post',
                        data: {
                            id: id,
                            is_recommend: isRecommend
                        },
                        dataType: 'json',
                        success: function(res) {
                            if (res.code) {
                                layui.popup.failure(res.msg);
                                obj.elem.checked = !obj.elem.checked; // 如果失败，恢复原状态
                                form.render('checkbox'); // 重新渲染开关
                            } else {
                                layui.popup.success('操作成功');
                            }
                        },
                        error: function() {
                            layui.popup.failure('请求失败');
                            obj.elem.checked = !obj.elem.checked; // 如果失败，恢复原状态
                            form.render('checkbox'); // 重新渲染开关
                        }
                    });
                });


				
				// 渲染表格
                table.render({
                    elem: "#data-table",
                    url: SELECT_API,
                    page: true,
                    cols: [cols],
                    skin: "line",
                    size: "lg",
                    toolbar: "#table-toolbar",
                    autoSort: false,
                    defaultToolbar: [{
                        title: "刷新",
                        layEvent: "refresh",
                        icon: "layui-icon-refresh",
                    }, "filter", "print", "exports"],
                    done: function () {
                        // Re-render the form to initialize dropdowns
                        form.render('select');
                        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
                    }
                });
				
				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    }
                });

                form.on('select(statusSelect)', function(obj) {
                    let id = obj.elem.getAttribute('data-id'); // 获取当前行的 ID
                    let status = obj.value; // 获取选中的值

                    // 调用接口保存更改
                    $.ajax({
                        url: UPDATE_API,
                        type: 'post',
                        data: {
                            id: id,
                            status: status
                        },
                        dataType: 'json',
                        success: function(res) {
                            if (res.code) {
                                layui.popup.failure(res.msg); // 显示失败提示
                            } else {
                                layui.popup.success('状态更新成功'); // 显示成功提示
                            }
                        },
                        error: function() {
                            layui.popup.failure('请求失败'); // 请求失败时提示
                        }
                    });
                });

                table.on("edit(data-table)", function(obj) {
                    if (obj.field === "price") { // 判断是否是价格栏
                        let data = {
                            id: obj.data[PRIMARY_KEY], // 获取当前行的主键
                            price: obj.value // 获取编辑后的值
                        };

                        // 调用接口保存更改
                        $.ajax({
                            url: UPDATE_API,
                            type: "post",
                            data: data,
                            dataType: "json",
                            success: function(res) {
                                if (res.code) {
                                    layui.popup.failure(res.msg); // 如果失败，显示错误信息
                                } else {
                                    layui.popup.success('操作成功');
                                }
                            },
                            error: function() {
                                layui.popup.failure("请求失败"); // 请求失败时提示
                            }
                        });
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    $('input[name=is_recommend]').val(0)
                    table.reload("data-table", {
                        where: []
                    })
                });
                
                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"800px", common.isModile()?"100%":"600px"],
                        content: INSERT_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"800px", common.isModile()?"100%":"600px"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
            })

        </script>
    </body>
</html>
